<?php

class Company extends Controller 
{
	var $uid;
	var $tpl;
    var $dbdata;

	function setVars()
	{
		$this->tpl['title'] = "Company info";
		$this->tpl['head'] = '';
        $this->uid = $this->session->userdata('uid');
        $this->oid = $this->session->userdata('org_id');
        $this->table = 'contact_org';
        $this->getdb();
        
        //$this->tpl['tpljs'][] = 'prototype';
        //$this->tpl['tpljs'][] = 'scriptaculous';
	}
	
	function __construct()
	{
		parent::Controller();
		log_message('debug', "Contact->Company: Controler Class Initialized");
        $this->auth->CheckLogin();
		$this->setVars();
        $this->load->model('Master_Model', 'mdb');
        //$this->output->enable_profiler(TRUE);
	}

	function index() 
	{
		if(isset($_POST['submit_org']))
		{
			$this->postCheck();
		}
		else
		{
            $this->tpl['country_select'] = $this->mdb->countrySelect('US');
			$this->load->view('contact/company', $this->tpl);
		}
	}

	function postCheck()
	{
		$this->load->library('validation');
		$rules['Name']			= "trim|required|min_length[5]|max_length[50]|xss_clean";
		$rules['Org_Type']		= "trim|required|xss_clean";
        $rules['Street']		= 'trim|required|min_length[5]|max_length[50]|xss_clean';
        $rules['Postcode']		= 'trim|required|max_length[12]|xss_clean';
		$rules['Country']		= "trim|required|xss_clean";
        $rules['Phone']		    = 'trim|required|max_length[15]|xss_clean';
        $rules['FAX']		    = 'trim|max_length[15]|xss_clean';
        $rules['Website']		= 'trim|required|min_length[6]|max_length[200]|xss_clean|prep_url';
		$fields['Name']    	= 'Company Name';
		$this->validation->set_rules($rules);
		$this->validation->set_fields($fields);
		if ($this->validation->run() == FALSE)
		{
            $data = (array) $this->dbdata;
            foreach($data as $k => $v){
                if(isset($this->validation->$k)){
                    $data[$k] = $this->validation->$k;
                }
            }
            $this->tpl = array_merge($this->tpl, $data);
            $this->tpl['org_type_select'] = org_type_form($this->validation->Org_Type);
            $this->tpl['country_select'] = $this->mdb->countrySelect($this->validation->Country);
			$this->load->view('contact/company', $this->tpl);
		}
		else
		{
            //$$this->validation->Phone = $this->fun->format_phone($this->validation->Phone);
            //$this->validation->FAX    = $this->fun->format_phone($this->validation->FAX);
            $data = (array) $this->dbdata;
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    if(strstr($k, 'Phone') && isset($this->validation->$k)){
                        $this->validation->$k = $this->fun->format_phone($this->validation->$k);
                    }
                    if(strstr($k, 'FAX') && isset($this->validation->$k)){
                        $this->validation->$k = $this->fun->format_phone($this->validation->$k);
                    }
                    $data[$k] = $this->validation->$k;
                }else{
                    $data[$k] = '';
                }
            }
            $dbd = (array) $this->dbdata;
            $data = new_value($dbd, $data);
            $this->db->where('id', $this->oid);
            $this->db->update('contact_org', $data);
            $this->tpl = array_merge($this->tpl, $dbd);
            $this->tpl = array_merge($this->tpl, $data);
            $this->tpl['org_type_select'] = org_type_form($this->validation->Org_Type);
            $this->tpl['country_select'] = $this->mdb->countrySelect($this->validation->Country);
            $this->msg->setMsg('Updated');
            $this->load->view('contact/company', $this->tpl);
            //redirect('contact/company', 'location');
		}
	}
	
    function getdb()
    {
        $query = $this->db->getwhere('contact_org', array('id' => $this->oid), 1);
        if ($query->num_rows() == 0)
        {   # Create if it doesn't excist so we can work with update insted of insert.
            $d = date("Y-m-d");
            $sql = "INSERT INTO contact_org (first) VALUES ('$d')";
            $this->db->query($sql);
            $this->oid = $this->db->insert_id();
            $dataoid = array('org_id' => $this->oid);
            $this->db->update('contact_user', $dataoid, array('uid' => $this->uid));
            $query = $this->db->getwhere('contact_org', array('id' => $this->oid), 1);
        } 
        $dbdata = $query->row();
        $this->dbdata = delids($dbdata);
        $dbdata = (array) $this->dbdata;
        $this->tpl = array_merge($this->tpl, $dbdata);
        $this->tpl['org_type_select'] = org_type_form($dbdata['Org_Type']);
    }
}
?>
